<div id="left_nav" class="span3" style="padding: 0px;">
    <div id="nav_group" class="inner_shadow">
        <?php
        echo $this->element('bank/nav_branch_sup');
        ?>
    </div>
</div>

<div class="span8" style="padding:10px; background-color: #fff; min-height: 480px;">
    <div class="page-header">
        <h3 class="remove-line-height">Credit Authorization</h3>
    </div>

    <table class="table table-striped data-table table-bordered">
        <thead>
        <tr>
            <th colspan="2">Date</th>
            <th>Generated By</th>
            <th>Narration</th>
            <th>Option</th>
        </tr>
        </thead>
        <tbody>
        <?php
        if(!empty($transactData)){

            $count = $this->Paginator->counter("%start%");
            foreach( $transactData as $val ) {
                $log_id = $val['BankCreditAuthorization']['id'];
                $student = $val['Transaction']['StudentUser']['index_number'].' ('.$val['Transaction']['StudentUser']['fname'].' '.$val['Transaction']['StudentUser']['lname'].')';
                $auth_amt = $val['BankCreditAuthorization']['authorize_amount'];
                $status = $val['BankCreditAuthorization']['status'];
                $trans_id = $val['Transaction']['id'];
                $stu_id = $val['Transaction']['StudentUser']['id'];
                $payment_method = $val['Transaction']['TransactionBankDetail'][0]['payment_method'];
                $revert_str = '';
                $allow_auth = true;
                if($val['BankCreditAuthorization']['reverted'] == 'Reverted'){
                    $allow_auth = false;
                    $revert_str = ' But '.$val['BankCreditAuthorization']['reverted'];
                }

                $authorized_date = date("d M Y, g:ia", strtotime($val['BankCreditAuthorization']['modified']));
                ?>
                <tr class="data-row" data-stu-id="<?php echo $stu_id; ?>" data-trans-id="<?php echo $trans_id; ?>"  data-student="<?php echo $student; ?>" data-amount="<?php echo $auth_amt; ?>"  data-payment_method="<?php echo $payment_method; ?>">
                    <td><?php echo $count++.'.'; ?></td>
                    <td><?php echo  date("d M Y, g:ia", strtotime($val['BankCreditAuthorization']['created'])); ?></td>
                    <td><?php echo $val['BankUser']['fname'].' '.$val['BankUser']['mname'].' '.$val['BankUser']['lname']." (".$val['BankUser']['BankBranch']['branch_name_full']." Branch)"; ?></td>

                    <?php
                        if($status == 'Not Authorized'){
                            if($allow_auth){
                    ?>
                                <td><?php echo "Credit an amount of <strong> GHS ".$auth_amt."</strong> to : <strong>".$student."</strong>"; ?></td>
                                <td ><a data-amount="<?php echo $auth_amt;?>" data-student="<?php echo $student?>" href="<?php echo $this->Html->url(array('controller'=>'Bank','action' => 'authorize/'.$log_id)); ?>" class="btn btn-primary btn-mini credit_authorize" ><i class="icon-key"></i> Authorize </a></td>
                                <td ><a data-amount="<?php echo $auth_amt;?>" data-student="<?php echo $student?>" href="<?php echo $this->Html->url(array('controller'=>'Bank','action' => 'credit_reversal/'.$log_id)); ?>" class="btn btn-danger btn-mini revert_btn" ><i class="icon-undo"></i> Revert </a></td>
                    <?php
                            }
                            else{
                    ?>
                                <td><?php echo "Credit an amount of GHS ".$auth_amt." to : ".$student." <strong>".$revert_str."</strong>"; ?></td>
                                <td colspan="2"><?php echo $status; ?></td>
                    <?php
                            }
                    ?>

                    <?php
                        }
                        else{
                    ?>
                            <td><?php echo "Credited GHS ".$auth_amt." to : ".$student.".<strong> $revert_str </strong>"; ?></td>
                            <td colspan="2"><?php echo $status. ' - '.$authorized_date; ?></td>

                    <?php
                        }
                    ?>

                </tr>
            <?php
            }
        }
        else{
            ?>
            <tr>
                <td colspan="6" class="cell1">
                    <div>
                        No transaction histories.
                    </div>
                </td>
            </tr>
        <?php
        }
        ?>
        </tbody>
    </table>

    <div class="pagination">
        <ul>
            <?php
            echo $this->Paginator->first('First', null, null, array('tag' => 'li'));
            //echo $paginator->prev('Previous', null, null, array('tag'=>'li'));
            echo $this->Paginator->numbers(array('separator' => '', 'tag' => 'li'));
            //echo $paginator->next('Next', null, null, array('tag'=>'li'));
            echo $this->Paginator->last('Last', null, null, array('tag' => 'li'));
            ?>
        </ul>
    </div>
</div>

<div class="modals">

    <div id="trans_reversal_modal" class="modal hide fade">
        <form class="form form-horizontal" method="post" action="<?php echo $this->Html->url(array('controller' => 'Bank','action' => 'credit_reversal')); ?>" enctype="" style="margin: 0px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4>Revert Credit Transaction</h4>
            </div>
            <div class="modal-body">

                <p>
                    You are about to revert the selected <span class="info" id="pay_method"></span> transaction,
                    debit the student account by GHC <span class="info" id="amount"></span>.
                </p>

                <p>If you will like to proceed, please specify the reason for the reversal below: <br /><br /></p>

                <div class="control-group">
                    <label class="control-label" for="address">Revert Reason:</label>
                    <div class="controls">
                        <textarea name="Transaction[revert_reason]" id="revert_reason" class="input" required=""></textarea>
                    </div>
                </div>

                <input type="hidden" name="Transaction[id]" id="transaction_id" value="" />
                <input type="hidden" name="Transaction[student_user_id]" id="student_id" value="" />
                <input type="hidden" name="Transaction[debit_amt]" id="transaction_debit_amt" value="" />

            </div>
            <div class="modal-footer">
                <button type="submit" href="<?php echo $this->Html->url(array('action' => 'credit_account')); ?>" id="revert_btn" class="btn btn-danger">Revert</button>
                <a href="#trans_reversal_modal" class="btn" data-toggle="modal">Close</a>
            </div>
        </form>
    </div>

    <div id="message_modal" class="modal hide fade">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4>Message Title</h4>
        </div>
        <div class="modal-body">
            Message Body
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal" id="message_modal_close_callback">Close</a>
        </div>
    </div>
</div>

<input type="hidden" id="print_history" value="<?php echo $this->Html->url(array('controller' => 'Student', 'action' => 'print_history')); ?>" />

<script type="text/javascript">


</script>

<?php echo $this->Html->script('bank/credit_auth'); ?>